﻿$(function () {
    $.kish.ApproveAnnDetail = {};
    $.extend($.kish.ApproveAnnDetail, {

        //********************************//
        //set basid details
        carID: null,
        IsInCarStore: null,
        initializing: function () {

            $.kish.ApproveAnnDetail.carID = $.Jahanutility.getParameterByName("ID");
            $.kish.ApproveAnnDetail.IsInCarStore = $.Jahanutility.getParameterByName("CarStore");
            if ($.kish.ApproveAnnDetail.carID != "0" && $.kish.ApproveAnnDetail.carID != '') {
                $.kish.ApproveAnnDetail.ShowDetails($.kish.ApproveAnnDetail.carID);
            };

            $(".AppAnnSubmit").click(function () {
                $.kish.ApproveAnnDetail.ApprCarAnn();
            });
        },
        //****************************//
        ShowDetails: function (carID) {

            // $('#submit').before('<img src="../../images/ajax-loader.gif" class="loader" />')            

            $('#submit').attr('disabled', 'disabled');
            $.ajax({
                'type': 'POST',
                'url': '../../KishCar/Services/CarAnn.svc/GetCar',
                'data': JSON.stringify({ CarID: carID }),
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'success': function (result) {
                    var CarData = result['d'];

                    $(".price").text($.trim(CarData.Price));
                    $(".year").text($.trim(CarData.ProductYear));
                    $(".usage").text($.trim(CarData.TraveledDistance));
                    $(".commentTxt").text($.trim(CarData.Description));
                    $(".model").text($.trim(CarData.Model.Code));
                    $(".color").text($.trim(CarData.Color.Code));
                    $(".brand").text($.trim(CarData.Model.Brand.Code));
                    $(".state").text($.trim(CarData.Status.Code));
                    $.each(CarData.CarImages, function () {
                        var relatedPath = '../' + this.Path.replace('~', '..');
                        var Image = '<a  class="highslide inline-block margin_l_15" href="' + relatedPath + '"> <img  class="width120 fn_prodImg " alt="' + CarData.Model.Code + '" title="تصویر بزرگتر" src= "' + relatedPath + '"/></a>';
                        $('.CarImagesDetail').append(Image)
                    });

                    $('#message').slideDown();
                    $('img.loader').fadeOut('slow', function () { $(this).remove() });
                    $('#submit').removeAttr('disabled', 'disabled');

                    $('a.highslide').click(function () {
                        return hs.expand(this)
                    });
                },
                'error': function (error) {
                    $('#submit').removeAttr('disabled');
                    $('img.loader').fadeOut('slow', function () { $(this).remove() });
                }
            });
        },
        ApprCarAnn: function () {
            $.kish.message.getConfirmationMessage(2, function () {
                $.ajax({
                    'type': 'POST',
                    'url': '../../KishCar/Services/CarAnn.svc/ActivatCar',
                    'data': JSON.stringify({ CarID: $.kish.ApproveAnnDetail.carID, IsInCarStore: $.kish.ApproveAnnDetail.IsInCarStore }),
                    'contentType': 'application/json; charset=utf-8',
                    'dataType': 'json',
                    'success': function (result) {
                        if (result['d'].IsSucceed) {
                            window.location.href = 'ApproveCarAnn.aspx';
                        }
                        else {
                            $.kish.message.showErrorMessage(result['d'].Message.Message);
                        };
                        $('#message').slideDown();
                        $('img.loader').fadeOut('slow', function () { $(this).remove() });
                        $('#submit').removeAttr('disabled', 'disabled');
                    },
                    'error': function (error) {
                        $('#submit').removeAttr('disabled');
                        $('img.loader').fadeOut('slow', function () { $(this).remove() });
                    }
                });
            });
        }
    });
    $.kish.ApproveAnnDetail.initializing();
});